<?php

class StudentPlace_NumberController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        if (!$yearSemester = $this->_getParam('year_semester'))
        $yearSemester = $this->view->Semester['yearSeme'];
        $this->view->yearSemester = $yearSemester;

        $classSn = $this->_getParam('classSn');
    }

    public function getStudentAction()
    {
        $this->getHelper('layout')->disableLayout();
        $tempArr = explode("-",$this->_getParam('year_semester'));
        $year = (int) $tempArr[0];
        $semester = (int) $tempArr[1];
        $tempArr = explode("-", $this->_getParam('classSn'));
        $grade = (int) $tempArr[0];
        $classSn = (int) $tempArr[1];

        $q = Doctrine_Query::create()
        ->select('a.sn, a.name, a.sex, a.study_condition, b.student_id AS student_id, b.number AS number')
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->where('b.year=?', $year)
        ->andWhere('b.semester=?', $semester)
        ->andWhere('b.grade=?', $grade)
        ->andWhere('b.class_sn=?', $classSn)
        //        ->andWhere('a.study_condition=?', Student_Model_Student::IN_REGISTER)
        ->orderBy('b.number');
        $res = $q->fetchArray();
        $arr = array();
        $i = 1;
        foreach ($res as $row) {
            if ($i < $row['number']) {
                for ($j=$i; $j<$row['number']; $j++)
                $arr[] = array('number'=>$j);
                $i = $j;
            }
            $arr[] = $row;
            $i++;
        }

        $f = new Zend_Filter_StripTags();
        $this->view->year_semester = $f->filter($this->_getParam('year_semester'));
        $this->view->classSn = $f->filter($this->_getParam('classSn'));
        $this->view->data = $arr;

    }

    public function setAction()
    {
        $tempArr = explode("-",$this->_getParam('year_semester'));
        $year = (int) $tempArr[0];
        $semester = (int) $tempArr[1];
        $tempArr = explode("-", $this->_getParam('classSn'));
        $grade = (int) $tempArr[0];
        $classSn = (int) $tempArr[1];
        // 調出此班
        $tempArr = $this->_getParam('move_out');
        if ($tempArr)
        $moveOut = array_keys($tempArr);
        else
        $moveOut = array();

        $snArr = $this->_getParam('sn');
        // print_r()
        foreach ($snArr as $sn=>$number) {
            // if (!in_Array($sn, $moveOut)) {
            $s = new Student_Model_StudentSemester();
            $studentSn = (int)substr($sn,1);
//            echo $studentSn.'--';
            if ($studentSn) {
                $row = $s->getTable()->findByDql('year=? AND semester=? AND student_sn=?',
                array($year,$semester, $studentSn))->getFirst();
                if ($row) {
                    $row->number = $number;
                    $row->save();
                }
            }
        }
        $className =  $row->class;
        // 刪除調出此班學生
        // 學期記錄包含事假記錄,刪除後無法回復,
        //        foreach ($moveOut as $sn) {
        //            $q = Doctrine_Query::create()
        //            ->delete('Student_Model_StudentSemester')
        //            ->where('year=?', $year)
        //            ->andWhere('semester=?', $semester)
        //            ->andWhere('student_sn=?', $sn)
        //            ->execute();
        //        }
        // 新增學生
        $newStudent = $this->_getParam('new_student_id');
        foreach($newStudent as $sn=>$studentId) {
            if (!$studentId)  continue;
            $studentSemester = new Student_Model_StudentSemester();
            $studentSemester->year = $year;
            $studentSemester->semester = $semester;
            $studentSemester->student_sn = $sn;
            $studentSemester->student_id = $studentId;
            $studentSemester->grade = $grade;
            $studentSemester->class_sn = $classSn;
            $studentSemester->class = $className;
            $studentSemester->number = $snArr[$sn];
            $studentSemester->save();
        }
        $this->_helper->json(true);
    }

    public function getStudentByIdAction()
    {
        $f = new Zend_Filter_StripTags();
        $yearSemesterArr = explode("-", $this->_getParam('year_semester'));
        $year = (int) $yearSemesterArr[0];
        $semester = (int) $yearSemesterArr[1];
        $tempArr = explode("-", $this->_getPAram('classSn'));
        $grade = (int)$tempArr[0];
        $classSn = (int)$tempArr[1];

        $studentId = $f->filter($this->_getParam('id'));

        // 查詢此學號是否存在於本學期
        $res = Doctrine_Query::create()
        ->select('a.sn, a.name, b.student_id AS semester_student_id')
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->where('b.year=?', $year)
        ->andWhere('b.semester=?', $semester)
        ->andWhere('a.student_id=?', $studentId)
        ->andWhere('a.study_condition=?', Student_Model_Student::IN_REGISTER)
        ->fetchOne();
        if ($res) {
            $this->_helper->json(array('success'=>false,'data'=>$res->name));
        }
        else {
            $res = Doctrine_Query::create()
            ->select('sn, name, sex, study_condition')
            ->from('Student_Model_Student')
            ->where('student_id=?', $studentId)
            ->andWhere('study_condition=?', Student_Model_Student::IN_REGISTER)
            ->fetchOne(null,Doctrine_Core::HYDRATE_ARRAY);
            if ($res)
            $arr['success'] = true;
            else
            $arr['success'] = false;
            $arr['data'] = $res;
            $this->_helper->json($arr);
        }

    }
}

